<template>
  <div>
    <time :time="time" @runTimeEmpty="runTimeEmpty" :isTimeStop="isTimeStop"></time>
    <component v-is="type['type']" :paper="paper" @changeAnswer="changeAnswer"></component>
    <button @click="nextPaper">下一题</button>
  </div>
</template>

<script>
/*
 基础模块
 1，给一个数据，渲染出一个题目，选中一个答案，提交

 叠加模块
 1,计时,描述:每道题给一个时间，时间到了自动下一题，如果是最后一题则自动交卷
 2,防作弊,描述:用户点到了其他地方会有一个弹窗警告，次数会叠加，到达一定次数会自动提交试卷，弹窗出来的时候计时是停止的
 3,我希望是每页必填的，如果用户不选择答案，则下一页按钮为不能选状态
 4,我希望计时的题目是不能够点击上一页的，不计时的题目是可以点击上一页的,并且点击上一页能回到上一题，而且显示上一题的答案，而且先保存本题的答案再回到上一题
*/
export default {
  data: {
    return () {
      isPractice: true,
      isLimitCheckout: true,
      isNeedTime: true,
      type: 0,
      isNeedCheating: true,
      data: {},
      cheatingSum: 0
    }
  },
  mounted () {
    this.getPaper()
  },
  methods () {
    getPaper () {
      paperDao.getData({id: this.route.query.id}).then(r => {
        this.data = r.response.paper
        this.time = r.response.time
      })
    },
    nextPaper (answer) {
      paperDao.nextPaper(answer).then(r => {
        this.getPaper()
      })
    },
    runTimeEmpty () {
      this.getPaper()
    },
    addPreventCheating () {
      window.addEventListener('blur', () => {
        if (this.cheatingWarnModal === true) {
          // 记录行为
          paperDao.addBehavier().then(r => {
            this.cheatingSum++
          })
        } else {
          // 记录作弊
          paperDao.cheat().then(r => {
            this.cheatingSum++
            if (this.cheatingSum === r.response.cheatingMax) {
              this.submitPaper()
              this.$route.push('paper-success')
            } else {
              this.cheatingWarnModal = true
              this.isTimeStop = isTimeStop
            }
          })
        }
      })
    },
    submitPaper () {
      paperDao.submitPaper().then(r => {
        console.log('试卷提交成功')
      })
    },
    cheatingWarnModalConfirm () {
      this.cheatingWarnModal = false
      paperDao.resumeTime().then(r => {
        this.time = r.response.time
      })
    },
    changeAnswer (val) {
      this.answer = answer
    },
    toPrePaper () {
      paperDao.saveAnswer().then(r => {
        console.log('成功保存本页答案')
      })
      this.getData()
    }
  },
  computed () {
    isNextDisabled () {
      const isnextdisabled = true
      if (this.answer === true) {
        isnextdisabled = true
      } else {
        isnextdisabled = false
      }
      return isnextdisabled
    },
    isPrvShow () {
      const isprvshow = true
      if (isDawu === true) {
        isprvshow = true
      } else {
        isprvshow = false
      }
      return isprvshow
    }
  }
}
</script>